
//
//  MemberEditVC.swift
//  物料商行
//
//  Created by HaoDong Hong on 2017/9/8.
//  Copyright © 2017年 HaoDong Hong. All rights reserved.
//

import UIKit
import Eureka
import SVProgressHUD

protocol MemberEditDelegate: class {
    func didChanged(authority: Int?, name: String)
}
class MemberEditVC: FormViewController, UITextFieldDelegate {
    
    var member: Member?
    weak var delegate: MemberEditDelegate?
    var authority = 0
    var name = ""
    var highlight = false
    let helper = StoreVCHelper()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        ButtonRow.defaultCellSetup = { cell, row in
            if self.member?.memberId == User.phoneNumber {
                cell.isHidden = true
            }
        }
        
        TextRow.defaultCellSetup = { cell, row in
            row.cell.textField.delegate = self
            
        }
        
        
        form +++
            
            Section("修改用户权限")
            
            <<< SegmentedRow<String>() {
                $0.options = ["管理员", "成员"]
                
                if member?.authority == 0 {
                    $0.value = "管理员"
                } else {
                    $0.value = "成员"
                }
            }
                .onChange({ [weak self] (row) in
                    if row.value == "管理员" {
                        self?.authority = 0
                    } else {
                        self?.authority = 1
                    }
                    print(row.value!)
                    SVProgressHUD.show()
                    self?.helper.editMember(id: (self?.member?.id)!, name: (self?.member?.name)!, authority: (self?.authority)!, completeHandler: { (success) in
                        if success {
                            SVProgressHUD.showSuccess(withStatus: "成功修改权限")
                        } else {
                            SVProgressHUD.showError(withStatus: "修改权限失败")
                        }
                        SVProgressHUD.dismiss(withDelay: 1.5)
                    })
                })
            +++ Section("修改用户信息")
                <<< TextRow() {
                    $0.title = "备注姓名"
                    $0.value = member?.name
                }
                    .onChange({ (row) in
                        
                    })
                    .onCellHighlightChanged({ (cell, row) in
                        if self.highlight {
                            SVProgressHUD.show()
                            self.helper.editMember(id: (self.member?.id)!, name: (row.value ?? ""), authority: self.authority, completeHandler: { (success) in
                                if success {
                                    SVProgressHUD.showSuccess(withStatus: "成功修改用户信息")
                                } else {
                                    SVProgressHUD.showError(withStatus: "修改用户信息失败")
                                }
                                SVProgressHUD.dismiss(withDelay: 1.5)
                            })
                        }
                        self.highlight = !self.highlight
                    })
            +++ Section()
            <<< ButtonRow() {
                $0.title = "删除成员"
                
            }
                .onCellSelection({ [weak self] cell, row in
                    SVProgressHUD.show()
                    self?.helper.deleteMember(id: [(self?.member?.id)!], completeHandler: { (success) in
                        print(success)
                        if success {
                            SVProgressHUD.showSuccess(withStatus: "成功移除该用户")
                        } else {
                            SVProgressHUD.showError(withStatus: "移除用户失败")
                        }
                        SVProgressHUD.dismiss(withDelay: 1.5)
                        self?.navigationController?.popViewController(animated: true)
                    })
                })
    }
    

}

